const express = require('express')
const multer = require('multer')
const { UPLOAD_PATH } = require('../utils/constant')
const Result = require('../models/Result')
const Enroll = require('../models/Enroll')
const boom = require('boom')
const { decoded } = require('../utils')
const enrollService = require('../services/enroll')

const router = express.Router()
//获取报名信息列表

router.get('/EnrollList', function(req, res, next) {
    enrollService.EnrollList(req.query)
      .then(({ list, count, page, pageSize }) => {
        new Result({ list, count, page: +page, pageSize: +pageSize },
          '获取报名信息成功').success(res)
      }).catch(err => {
      next(boom.badImplementation(err))
    })
  })
//获取user的报名状态
router.get('/ExamineList', function(req, res, next) {
enrollService.ExamineList(req.query)
    .then((list) => {
        new Result(list,
        '获取审核信息成功').success(res)
    }).catch(err => {
    next(boom.badImplementation(err))
})
})
//获取管理员审核的用户全部报名状态
router.get('/listExamineEnroll', function(req, res, next) {
    enrollService.listExamineEnroll(req.query)
        .then((list) => {
            new Result(list,
            '获取审核信息1成功').success(res)
        }).catch(err => {
        next(boom.badImplementation(err))
    })
    })

router.get('/UserEnrollList', function(req, res, next) {
enrollService.UserEnrollList(req.query)
    .then(({ list, count, page, pageSize }) => {
    new Result({ list, count, page: +page, pageSize: +pageSize },
        '获取报名信息成功').success(res)
    }).catch(err => {
    next(boom.badImplementation(err))
})
})

//获取单个
router.get('/getOneEnroll', function(req, res) {
    const news = req.query.fileName
    //console.log(news)
    enrollService.getOneEnroll(news).then(News => {
        if (News) {
            new Result(News, ' 单个报名信息查询成功').success(res)
        } else {
            new Result('单个报名信息查询失败').fail(res)
        }
    })

})

//获取单个
router.get('/getOneExamine', function(req, res) {
    const news = req.query.fileName
    //console.log(news)
    enrollService.getOneExamine(news).then(News => {
        if (News) {
            new Result(News, ' 单个报名信息查询成功').success(res)
        } else {
            new Result('单个报名信息查询失败').fail(res)
        }
    })

})


//添加新闻图片
router.post('/uploadNews1', multer({ dest: `${UPLOAD_PATH}/news` }).single('file'),function(req, res, next) {
        //console.log(req.file)
        //console.log(req.body)
        //const News = req.file
        if (!req.file || req.file.length === 0) {
            new Result('上传图片失败').fail(res)
        } else {
            const newsImg = new News(req.file)
            //因为上传的文件没有jpg的图片后缀，所有加一个parse（）来重命名
            newsImg.parse()
                .then(newsImg => {
                    new Result(newsImg, '上传图片成功').success(res)
                })
                .catch(err => {
                    next(boom.badImplementation(err))
                })

        }
    }
)

//添加报名图片
router.post('/uploadEnroll', multer({ dest: `${UPLOAD_PATH}/enroll` }).single('file'),function(req, res, next) {
        //console.log(req.file)
        //console.log(req.body)
        //const News = req.file
        if (!req.file || req.file.length === 0) {
            new Result('上传图片失败').fail(res)
        } else {
            const newsImg = new Enroll(req.file)
            //因为上传的文件没有jpg的图片后缀，所有加一个parse（）来重命名
            newsImg.parse(req.body.username)
                .then(newsImg => {
                    new Result(newsImg, '上传图片成功').success(res)
                })
                .catch(err => {
                    next(boom.badImplementation(err))
                })

        }
    }
)
//添加报名信息
router.post('/createEnroll', function(req, res, next) {
        ////console.log(req.body)
        const news = req.body
        var danqian=new Date().valueOf()//获取当前时间
        var kaishi=new Date("2023/3/30 09:00").valueOf()
        var jieshu=new Date("2023/4/06 18:00").valueOf() 
        console.log(danqian+"hh:"+kaishi+"hu"+jieshu)
        if(danqian>=kaishi&&danqian<=jieshu){
            enrollService.insertEnroll(news).then(() => {
                new Result('添加报名信息成功').success(res)
            }).catch(err => {
                next(boom.badImplementation(err))
            })
        }else{
            next(boom.badImplementation("不在报名时间"))
        }
    }
)
//添加审核信息
router.post('/createEnrollExamine', function(req, res, next) {
    ////console.log(req.body)
    const news = req.body
    let d = new Date()
    news.examine_time=d.getTime()
    enrollService.insertEnrollExamine(news).then(() => {
        new Result('添加审核信息成功').success(res)
    }).catch(err => {
        next(boom.badImplementation(err))
    })
}
)
//修改报名信息
router.post('/updateEnroll', function(req, res, next) {
        ////console.log(req.body)
        const news = req.body
        var danqian=new Date().valueOf()//获取当前时间
        var kaishi=new Date("2023/3/30 09:00").valueOf()
        var jieshu=new Date("2023/4/06 18:00").valueOf() 
        console.log(danqian+"hh:"+kaishi+"hu"+jieshu)
        if(danqian>=kaishi&&danqian<=jieshu){
            enrollService.updateEnroll(news).then(() => {
                new Result('修改报名信息成功').success(res)
            }).catch(err => {
                next(boom.badImplementation(err))
            })
        }else{
            next(boom.badImplementation("不在报名时间"))
        }
        }
)
//修改报名信息（管理员不受时间限制）
router.post('/updateEnroll1', function(req, res, next) {
    ////console.log(req.body)
    const news = req.body
    

        enrollService.updateEnroll(news).then(() => {
            new Result('修改报名信息成功').success(res)
        }).catch(err => {
            next(boom.badImplementation(err))
        })
   
    }
)
//删除报名信息
router.get('/deleteEnroll', function(req, res, next) {
    const id = req.query.id
    ////console.log(req)
    if (!id) {
        next(boom.badRequest(new Error('参数id不能为空')))
    } else {
        enrollService.deleteEnroll(id).then(() => {
            new Result('删除报名信息成功').success(res)
        }).catch(err => {
            next(boom.badImplementation(err))
        })
    }
})


module.exports = router
